package com.liliudong.template.common.util;

import lombok.extern.slf4j.Slf4j;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;

/**
 * MappedStatementUtils
 *
 * @author ldli5
 * @classname MappedStatementUtils
 * @date 2024/6/19-12:28
 */
@Slf4j
public class MappedStatementUtils {

	public static  <T extends Annotation> T getMethodAnnotation(String mappedStatementId, Class<T> annotationClass) {
		String mapperClassName = mappedStatementId.substring(0, mappedStatementId.lastIndexOf('.'));
		String methodName = mappedStatementId.substring(mappedStatementId.lastIndexOf('.') + 1);
		try {
			Class<?> mapperClass = Class.forName(mapperClassName);
			Method[] methods = mapperClass.getDeclaredMethods();

			for (Method method : methods) {
				if (method.getName().equals(methodName)) {
					return method.getAnnotation(annotationClass);
				}
			}
		} catch (ClassNotFoundException e) {
			log.error("Class not found: {}", mapperClassName, e);
		}
		return null;
	}
}
